package com.hyperf.spring.boot.autoconfigure.snowflake;

import com.hyperf.core.snowflake.SnowflakeTemplate;
import com.hyperf.core.snowflake.WorkerRegister;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lizhaoyang
 */
@Configuration
@EnableConfigurationProperties(SnowflakeProperties.class)
@ConditionalOnBean(WorkerRegister.class)
@ConditionalOnProperty(prefix = "hyperf.snowflake", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter({
        RedisSnowflakeAutoConfiguration.class,
        ZookeeperSnowflakeAutoConfiguration.class
})
public class SnowflakeTemplateAutoConfiguration {

    @Bean(value = "snowflakeTemplate", initMethod = "start", destroyMethod = "destroy")
    @ConditionalOnMissingBean
    public SnowflakeTemplate snowflakeTemplate(WorkerRegister workerRegister) {
        return new SnowflakeTemplate(workerRegister);
    }
}
